/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package olimpus.genericos;

import java.io.Serializable;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.sql.DataSource;
import javax.naming.InitialContext;

/**
 *
 * @author Leonardo
 */
public class Coneccion implements Serializable
{
    private DataSource olimpus;
    private Connection con;

    public Coneccion() throws Exception
    {
        try 
        {
            Context c = new InitialContext();
            //Resource
            olimpus = (DataSource) c.lookup("Olimpus");
        }
        catch (Exception ex)
        {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
            throw new Exception("Coneccion--> " + ex.getMessage() + " [" + this.getClass().getSimpleName() + "]");
        }
    }

    public Connection getConeccion() throws Exception
    {
        try
        {
           if (con == null)
           {
               con = olimpus.getConnection();
               con.setAutoCommit(false);
               return con;
           }
           else
           {
               return con;
           }
        }
        catch(Exception ex)
        {
            Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
            throw new Exception("Coneccion--> " + ex.getMessage() + " [" + this.getClass().getSimpleName() + "]");
        }
    }
}
